package com.fanqiechaodan.strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fanqiechaodan
 * @Classname ShoppingCart
 * @Description 购物车类
 */
public class ShoppingCart {

    private List<Item> itemList;
    private PaymentStrategy paymentStrategy;

    public ShoppingCart() {
        itemList = new ArrayList<>();
    }

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void addItem(Item item){
        itemList.add(item);
    }

    public void removeItem(Item item){
        itemList.remove(item);
    }

    public double calculateTotal(){
        return itemList.stream().mapToDouble(Item::getPrice).sum();
    }

    public void pay(){
        paymentStrategy.pay(calculateTotal());
    }
}
